/**
 * @desc service-task
 * @author DragonTeam <https://www.bpmport.com>
 * @since 2024/11/7 14:16
 */
import type { BaseNodeBO } from '@/types'
import type { BpmnElement } from '@/types/bpmn-node'
import { createElement } from '@/data-transform/bpmn-transform/utils'

// bpmn 对应的特殊业务节点属性处理部分
export function transformBpmnServiceTask(nodeBo: BaseNodeBO, element: BpmnElement) {
  const extensionElements = createElement('bpmn:ExtensionElements', {
    values: [
    ],
  })
  element.set('extensionElements', extensionElements)

  // 设置服务任务扩展子类型（抄送、邮件等）
  element.set('type', nodeBo.type)

  // 区分不同类型的服务任务
  switch (nodeBo.type) {
    case 'copy':
      // 抄送任务的处理逻辑
      const { messageType, transferTo, messageContent } = nodeBo
      extensionElements.values.push(
        createElement('Field', { name: 'transferToUserNos', string: (transferTo as any[])?.map(i => i.code).join(',') }),
        createElement('Field', { name: 'messageType', string: (messageType as string[])?.join('') }),
        createElement('Field', { name: 'content', string: messageContent }),
      )
      return
    case 'mail':
      // 邮件任务的处理逻辑
      const { mailTo, subject, mailContent } = nodeBo
      extensionElements.values.push(
        createElement('flowable:Field', { name: 'to', expression: (mailTo as any[])?.map(i => i.email).join(',') }),
        createElement('flowable:Field', { name: 'subject', string: subject }),
        createElement('flowable:Field', { name: 'html', expression: mailContent }),
        createElement('flowable:Field', { name: 'charset', string: 'utf-8' }),
      )
      break
    default:
  }
}
